<template>
    <div class="app-container">
        <el-icon @click="clickSwitchPlatform">
            <Switch/>
        </el-icon>

        <!-- 选择系统平台对话框 -->
        <el-dialog :title="title" v-model="open" width="700px" :close-on-click-modal="false" append-to-body>
            <el-row>
                <!-- 表格数据 -->
                <el-table v-loading="loading" :data="platformList" ref="multipleTable" border>
                    <el-table-column label="系统平台" prop="platformName" width="450" />
                    <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                        <template #default="scope">
                            <el-button type="success" disabled v-if="scope.row.currentLogin == 'Y'">当前系统</el-button>
                            <el-button type="primary" v-else @click="handleSwitchPlatform(scope.row)">切换系统</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-row>
            <el-row style="padding-right: 100px;">
                <pagination
                    v-show="total > 0"
                    :total="total"
                    v-model:page="queryParams.pageNum"
                    v-model:limit="queryParams.pageSize"
                    @pagination="getSwitchPlatformList"
                />
            </el-row>

        </el-dialog>

    </div>
</template>

<script setup name="switchPlatform">

    import { selectPlatformListByUser, switchPlatform } from "@/api/system/platform";
    import useUserStore from "@/store/modules/user";

    const { proxy } = getCurrentInstance();
    const router = useRouter();
    const userStore = useUserStore();

    const open = ref(false);
    const loading = ref(false);
    const total = ref(0);
    const title = ref("");
    const platformList = ref([]);

    const data = reactive({
        queryParams: {
            pageNum: 1,
            pageSize: 10,
            roleName: undefined,
            roleKey: undefined,
            status: undefined
        }
    });

    const { queryParams } = toRefs(data);

    const redirect = ref(undefined);
    const visitedViews = computed(() => useTagsViewStore().visitedViews);

    // 点击首页切换系统平台图标
    function clickSwitchPlatform() {
        open.value = true;
        title.value = "切换系统平台";
        getSwitchPlatformList();
    }

    // 获取系统平台列表
    function getSwitchPlatformList() {
        loading.value = true;
        selectPlatformListByUser(queryParams.value).then(response => {
            loading.value = false;
            platformList.value = response.rows;

            total.value = response.total;
            loading.value = false;
        });
    }

    // 点击切换系统按钮
    function handleSwitchPlatform(row) {
        switchPlatform({ platformCode: row.platformCode}).then(response => {
            proxy.$tab.closeAllPage().then(({ visitedViews }) => {
                userStore.platform = row.platformCode;
                sessionStorage.setItem("platform", userStore.platform);
                open.value = false;
                router.push("/");
            });
        });
    }

</script>